package com.yu.waterstation.system.modules.system.controller;

import com.yu.waterstation.common.common.annotation.AdminLog;
import com.yu.waterstation.common.common.annotation.NeedPermission;
import com.yu.waterstation.common.common.constant.Const;
import com.yu.waterstation.common.common.exception.ServiceException;
import com.yu.waterstation.common.common.util.GeneratorUtil;
import com.yu.waterstation.common.common.util.result.Result;
import com.yu.waterstation.common.service.RedisService;
import com.yu.waterstation.system.modules.system.mapper.RolePermissionMapper;
import com.yu.waterstation.system.modules.system.mapper.SysUserMapper;
import com.yu.waterstation.common.entity.system.SysUser;
import com.yu.waterstation.common.entity.system.form.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @date 2020-11-29 18:33:56
 */
@Api(tags = "系统：系统管理员")
@RestController
@RequestMapping("/system/sysUser")
public class SysUserController {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Resource
    private RedisService redisService;


    @ApiOperation("查询全部")
    @NeedPermission("system:sysUser:list")
    @GetMapping("/list")
    public Result<List<SysUserVo>> getAllSysUsers() {
        List<SysUserVo> users = sysUserMapper.getAllUserVOs();
        return Result.ok(users);
    }

    @ApiOperation("根据id查询")
    @NeedPermission("system:sysUser:info")
    @GetMapping("/{sysUserId}")
    public Result detail(@PathVariable("sysUserId") Integer sysUserId) {
        return Result.ok(sysUserMapper.selectById(sysUserId));
    }

    @ApiOperation("增")
    @NeedPermission("system:sysUser:add")
    @PostMapping("")
    public Result add(@RequestBody SysUser sysUser) {
        sysUser.setPassword(GeneratorUtil.md5Base64("123456"));
        return Result.ok(sysUserMapper.insert(sysUser));
    }

    @ApiOperation("改")
    @AdminLog
    @NeedPermission("system:sysUser:update")
    @PutMapping("")
    public Result update(@RequestBody SysUser sysUser) {
        return Result.ok(sysUserMapper.updateById(sysUser));
    }

    @ApiOperation("批量删")
    @AdminLog
    @NeedPermission("system:sysUser:delete")
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> sysUserIdList) {
        return Result.ok(sysUserMapper.deleteBatchIds(sysUserIdList));
    }

    @ApiOperation("更新账号激活状态")
    @AdminLog
    @NeedPermission("system:sysUser:update")
    @PutMapping("/status/{sysUserId}/{status}")
    public Result updateStatus(@ApiParam("sysUserId") @PathVariable Integer sysUserId,
                               @ApiParam("status") @PathVariable Boolean status) {
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserId);
        sysUser.setStatus(status);
        if(!status)
            redisService.hdel(Const.CONST_sys_user_session_map, sysUserId.toString()); // 删除会话信息
        return Result.ok(sysUserMapper.updateById(sysUser));
    }

    @ApiOperation("重置密码为123456")
    @AdminLog
    @NeedPermission("system:sysUser:update")
    @PutMapping("/resetPassword/{sysUserId}")
    public Result resetPassword(@ApiParam("sysUserId") @PathVariable Integer sysUserId) throws ServiceException {
        SysUser sysUser = new SysUser();
        sysUser.setId(sysUserId);
        sysUser.setPassword(GeneratorUtil.md5Base64("123456"));
        return Result.ok(sysUserMapper.updateById(sysUser));
    }


    @ApiOperation("设置用户的角色")
    @AdminLog
    @NeedPermission("system:sysUser:update")
    @PutMapping("/role/{userId}/{roleId}")
    public Result setRoleOfSysUser(@PathVariable Integer userId, @PathVariable Integer roleId) throws ServiceException {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setRoleId(roleId);
        return Result.ok(sysUserMapper.updateById(sysUser));
    }
}
